package com.chisheng.caa.controller;

import com.chisheng.caa.pojo.ArrangeCourseSetting;
import com.chisheng.caa.pojo.response.JsonResponse;
import com.chisheng.caa.pojo.dto.ArrangeCourseSettingDTO;
import com.chisheng.caa.pojo.dto.CourseSchedulingDTO;
import com.chisheng.caa.service.ArrangeCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;

@CrossOrigin
@Api("排课服务控制器")
@RestController
@RequestMapping(value = "/api/caa/arrangement", produces = MediaType.APPLICATION_JSON_VALUE)
public class ArrangeCourseController {

    @Resource
    private ArrangeCourseService service;

    @ApiOperation("打开一份排课计划")
    @PostMapping("/open")
    public JsonResponse<String> open() {
        return JsonResponse.success(service.open());
    }

    @ApiOperation("设定排课参数")
    @PostMapping("/setting/set/{accessKey}")
    public JsonResponse<?> setSetting(@ApiParam("访问键") @PathVariable String accessKey,
                                      @ApiParam("排课设定信息") @Validated @RequestBody ArrangeCourseSettingDTO setting) {
        service.setSetting(accessKey, setting);
        return JsonResponse.success();
    }

    @ApiOperation("获取排课参数")
    @GetMapping("/setting/get/{accessKey}")
    public JsonResponse<ArrangeCourseSetting> getSetting(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.getSetting(accessKey));
    }

    @ApiOperation("添加排课")
    @PutMapping("/add/{accessKey}")
    public JsonResponse<?> add(@ApiParam("访问键") @PathVariable String accessKey,
                               @ApiParam("排课信息") @Validated @RequestBody CourseSchedulingDTO course) {
        service.add(accessKey, course);
        return JsonResponse.success();
    }

    @ApiOperation("移除排课")
    @DeleteMapping("/remove/{accessKey}/{courseCode}")
    public JsonResponse<?> remove(@ApiParam("访问键") @PathVariable String accessKey,
                                  @ApiParam("移除排课的课程代码") @PathVariable String courseCode) {
        service.remove(accessKey, courseCode);
        return JsonResponse.success();
    }

    @ApiOperation("更新排课")
    @PostMapping("/update/{accessKey}")
    public JsonResponse<?> update(@ApiParam("访问键") @PathVariable String accessKey,
                                  @ApiParam("修改的课程安排信息") @Validated @RequestBody CourseSchedulingDTO course) {
        service.modify(accessKey, course);
        return JsonResponse.success();
    }

    @ApiOperation("获取排课列表")
    @PostMapping("/list/{accessKey}")
    public JsonResponse<Collection<CourseSchedulingDTO>> list(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.getCollection(accessKey));
    }

    @ApiOperation("导出 Excel 排课课表")
    @PostMapping("/export/{accessKey}")
    public JsonResponse<String> export(@ApiParam("访问键") @PathVariable String accessKey) {
        return JsonResponse.success(service.export(accessKey));
    }

}
